import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    goodList: [
      {
        name: '诸葛亮',
        price: 1000,
        num: 1,
        checked: false
      },
      {
        name: '蔡文姬',
        price: 1500,
        num: 1,
        checked: false
      },
      {
        name: '妲己',
        price: 2000,
        num: 1,
        checked: false
      },
      {
        name: '鲁班',
        price: 2200,
        num: 1,
        checked: false
      }
    ]
  },
  mutations: {
    subcount ({ goodList }, ind) {
      goodList[ind].num++
    },
    addcount ({ goodList }, ind) {
      goodList[ind].num--
    },
    deleteitem ({ goodList }, ind) {
      goodList.splice(ind, 1)
    },
    changeitem ({ goodList }, ind) {
      // console.log(111)
      // console.log(goodList[ind].checked)
      goodList[ind].checked = !goodList[ind].checked
    },
    isAllbtn ({ goodList }, isall) {
      if (isall) {
        goodList.forEach(item => (item.checked = true))
      } else {
        goodList.forEach(item => (item.checked = false))
      }
    }
  },
  actions: {
  },
  getters: {
    sumcount ({ goodList }) {
      return (goodList.filter(item => item.checked === true)).reduce((sum, item) => (sum += item.num * item.price), 0)
    }
  },
  modules: {
  }
})
